package com.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.SQLException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.model.User;
import com.service.UserServiceImpl;


@WebServlet("/UserNameAJAX")
public class UserNameAJAX extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    public UserNameAJAX() {
        super();
    }

	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doPost(request, response);
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		request.setCharacterEncoding("UTF-8");
		response.setContentType("text/html;charset=utf-8");
		
		String username = request.getParameter("username");
		String msg = null;

		try {
			UserServiceImpl userService = new UserServiceImpl();
			if(userService.userNameAJAX(username)){
				msg = "<span style='color:red;'>用户名已存在</span>";
			} else {
				msg = "<span style='color:green;'>用户名可以使用</span>";
			}
			PrintWriter out = response.getWriter();
			out.write(msg);
			out.flush();
			out.close();
		} catch (SQLException e1) {
			e1.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

}
